package com.power.sifa.runner.schedule;

import com.plf.common.domain.YesNo;
import com.plf.common.utils.StringUtil;
import com.power.sifa.correction.model.Crc_person;
import com.power.sifa.correction.service.Crc_personService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.EnableScheduling;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@EnableScheduling
@Slf4j
public class CondeployScheduled {

    @Resource
    public Crc_personService crc_personService;

    /**
     * 取消人员布控定时任务, 每天0点1分执行
     */
    @Scheduled(cron = "0 1 0 * * ?")
//    @Scheduled(cron = "*/20 * * * * ?")
    public void cancelCondeploy() {
        log.info("取消人员布控定时任务开始");
        List<Crc_person> personList = this.crc_personService.queryListByIsFocus(YesNo.YES);
        Date now = new Date();
        // 到期了取消布控
        if(!CollectionUtils.isEmpty(personList)) {
            for (Crc_person crc_person : personList) {
                if(StringUtil.isEmpty(crc_person.getConenddate())) {
                    log.error("{} - 布控人员未设置布控结束时间", crc_person.getName());
                    continue;
                }
                // 判断布控时间是否小于当前时间, 是则取消布控
                if(crc_person.getConenddate().before(now)) {
                    crc_person.setFocus(YesNo.NO);
                    crc_person.setConstartdate(null);
                    crc_person.setConenddate(null);
                    this.crc_personService.saveOrUpdate(crc_person);
                    log.error("{} - 布控人员已解除布控", crc_person.getName());
                }
            }
        }
        log.info("取消人员布控定时任务结束");
    }
}
